<template>
  <NavBar fixed title="动态详情" />

  <div class="container">
    <div class="header">
      <var-image width="45px" height="45px" :radius="5" :src="dynamic.avatar" />
      <div class="right">
        <span>{{ dynamic.username }}</span>
        <span>{{ dayjs(dynamic.createdAt).fromNow() }}</span>
      </div>
    </div>
    <div v-if="dynamic.content" class="content">{{ dynamic.content }}</div>

    <div v-if="dynamic.community_img?.length" class="imgs">
      <var-image
        @click.stop="preview(img)"
        class="img"
        v-for="(img, index) in dynamic.community_img"
        :key="index"
        :src="img"
      />
    </div>

    <div class="comment">
      <div class="title" ref="commentRef">评论({{ commentTotal }})</div>
      <div class="item" v-for="item in commentList" :key="item.id">
        <var-image width="30px" height="30px" :radius="5" :src="item.avatar" />
        <div class="right">
          <div class="top">
            <span>{{ item.username }}</span>
            <span>{{ dayjs(item.createdAt).fromNow() }}</span>
          </div>
          <div class="content">{{ item.content }}</div>
        </div>
      </div>
    </div>
  </div>

  <div class="bottom">
    <var-icon
      v-show="!dynamic.isStar"
      color="var(--color-primary)"
      name="thumb-up-outline"
      @click="toStar(true)"
    />
    <var-icon
      v-show="dynamic.isStar"
      color="var(--color-primary)"
      name="thumb-up"
      @click="toStar(false)"
    />
    <div class="right">
      <var-input
        variant="outlined"
        :disabled="isBannded"
        :placeholder="isBannded ? '禁言中' : '说点什么吧...'"
        size="small"
        v-model="reply"
        @keyup.enter="handleReply"
      />

      <i
        @click="handleReply"
        class="iconfont icon-a-youjiantouzhixiangyoujiantou"
      ></i>
    </div>
  </div>
</template>

<script setup lang="ts">
import NavBar from '@/components/NavBar.vue'
import {
  getDynamicDetail,
  dynamicLike,
  getUserInfo,
  getCommentList,
  postComment
} from '@/api'
import dayjs from '@/utils/dayjs'
import { debounce } from '@/utils'
import { useInfoStore } from '@/store/info'

const route = useRoute()
const infoStore = useInfoStore()

const isBannded = ref(false)
const dynamic = ref<any>({})

const commentTotal = ref(0)
const commentList = ref([])

onMounted(async () => {
  const [userInfo, res, commentData] = await Promise.all([
    getUserInfo(),
    getDynamicDetail(route.params.id),
    getCommentList({
      communityId: +route.params.id
    })
  ])

  dynamic.value = res
  isBannded.value = !!(userInfo.status === 1)
  commentTotal.value = commentData.total
  commentList.value = commentData.list
})

const toStar = debounce(async val => {
  await dynamicLike(dynamic.value.id)
  dynamic.value.isStar = val
})

const preview = img => {
  ImagePreview(img)
}

const reply = ref('')
const commentRef = ref(null)

const handleReply = debounce(async () => {
  if (!reply.value.trim()) return
  const params = {
    userId: infoStore.userInfo.id,
    communityId: dynamic.value.id,
    content: reply.value
  }

  const res = await postComment(params)
  const commentData = await getCommentList({
    communityId: +route.params.id
  })
  commentTotal.value = commentData.total
  commentList.value = commentData.list
  Snackbar.success(res)
  reply.value = ''
  commentRef.value.scrollIntoView({ behavior: 'smooth' })
})
</script>

<style scoped lang="scss">
.container {
  padding: 60px 0;

  .header {
    display: flex;
    align-items: end;
    .right {
      margin-left: 5px;
      display: flex;
      flex-direction: column;
      span {
        &:last-child {
          font-size: 14px;
          color: #666;
        }
      }
    }
  }

  .content {
    color: #666;
    line-height: 1.5;
  }
  .content,
  .imgs {
    padding: 10px 0;
  }

  .img {
    padding: 5px 0;
  }

  .comment {
    .title {
      font-size: 18px;
      font-weight: bold;
      padding: 10px 0;
    }
    .item {
      display: flex;
      padding: 5px 0;
      border-bottom: 1px solid #f0f0f0;
      .right {
        margin-left: 5px;
        .top {
          display: flex;
          flex-direction: column;
          span {
            &:last-child {
              font-size: 12px;
              color: #666;
            }
          }
        }
        .content {
          color: #666;
          font-size: 14px;
        }
      }
    }
  }
}

.bottom {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 10px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-top: 1px solid #f0f0f0;

  .right {
    flex: 1;
    margin-left: 10px;
    display: flex;
    align-items: center;
    .var-input {
      flex: 1;
      margin-right: 5px;
    }
  }
}
</style>
